package com.vitria.m3oui.rest.app.mgr;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import junit.framework.TestCase;

public class PDTest extends TestCase{
	
	public static String dsUrlBase = "/m3oui/rest/app/ds/user/vtbaadmin/ds/{name}";
	
	private static DefaultHttpClient httpclient = null;
//	
//	private void sendEvents(DefaultHttpClient httpclient, String dsname){
//		String opString = dsUrlBase.replace("{name}", dsname) + "?op=confirm";
//		
//		List<NameValuePair> params = new ArrayList<NameValuePair>();
//		params.add(new BasicNameValuePair("table", "submitted")); 
//		params.add(new BasicNameValuePair("strategy", "fast"));		
//		HttpClient.post(httpclient, opString, params);
//		
//		params = new ArrayList<NameValuePair>();
//		params.add(new BasicNameValuePair("table", "created"));
//		params.add(new BasicNameValuePair("strategy", "fast"));
//		HttpClient.post(httpclient, opString, params);
//	}
	
	
	@Override
	public void setUp(){
		if(httpclient == null)
			httpclient = HttpClient.login("vtbaadmin", "vitria");
	}
	
	@Override
	public void tearDown(){
	}
	
	private void sendEvents(String dsname, String table){
		String opString = dsUrlBase.replace("{name}", dsname) + "?op=send";
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("table", table)); 
		params.add(new BasicNameValuePair("strategy", "fast"));
		HttpClient.post(httpclient, opString, params);
		
		opString = dsUrlBase.replace("{name}", dsname) + "?op=confirm";
		params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("table", table));
		params.add(new BasicNameValuePair("strategy", "fast"));
		HttpClient.post(httpclient, opString, params);
	}
	
	private ArrayList<String> listRunningModels(){
		String opString = "/m3oui/rest/app.mgr/apps/pd?op=list.models&status=Running";
		String response = HttpClient.get(httpclient, opString);
		ArrayList<String> models = new ArrayList<String>();
		try {
			Document dom = DocumentHelper.parseText(response);
			Element root = dom.getRootElement();
			List<Element> elements = root.elements("model");
			for(Element ele:elements){
				models.add(ele.attributeValue("id"));
			}
		} catch (DocumentException e) {			
			e.printStackTrace();
		}
		return models;
	}
	
	private void operateModel(String opt, String id){
		String opString = "/m3oui/rest/app.mgr/apps/pd?op="+opt+".model";
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("id", id)); 
		HttpClient.post(httpclient, opString, params);
	}
	
	private void upgradeApp(String uid){
		String opString = "/m3oui/rest/app.mgr/apps/pd?op=upgrade&uid="+uid;
		HttpClient.get(httpclient, opString);
	}
	
	public void testPDUpgrade(){
		int count = 100;
		sendEvents("DataSet","submitted");
		sendEvents("DataSet","created");
		ArrayList<String> models = listRunningModels();
		
		for(int i=0; i<count; i++){
			//stop pd model
			for(String model:models)
				operateModel("stop", model);
			
			//upgrade
			upgradeApp(i%2==0?"pd.2.0":"pd.1.0");
	
			//start pd model
			for(String model:models)
				operateModel("start", model);
			
			//send events
			sendEvents("DataSet","submitted");
			sendEvents("DataSet","created");
		}
	}
}
